<?php
if (!defined('IN_MEDIA')) die("Hacking attempt");
function box_main_menu() {
	global $tpl, $web_type,$mainURL;
			$home_URL		= url_link('','','Home');
			$topplay_URL	= url_link('','','TopPlay');
			$topdownload_URL= url_link('','','TopDownload');
			$listalbum_URL	= url_link('','','ListAlbum');
			$listuser_URL	= url_link('','','ListUser');
include("templates/".$_SESSION['current_tpl']."/boxes/main_menu.html");
}

function box_gift() {
	include("templates/".$_SESSION['current_tpl']."/boxes/gift.html");
}
function box_send_gift() {
	include("templates/".$_SESSION['current_tpl']."/gift.html");
}
function box_user_menu() {
	global $mysql, $isLoggedIn, $tpl,$web_type,$mainURL;
	if ($isLoggedIn) {
		$user_Logout	=  url_link('','','Logout');
		$user_NAME		=  m_get_data('USER',$_SESSION['user_id']);
		$identifier		=	m_get_data('USER',$_SESSION['user_id'],'user_identifier');
		$user_CP		=  $mainURL.'/memcp.php?id='.$identifier;
		include("templates/".$_SESSION['current_tpl']."/boxes/user_logged.html");
	}
	else {
		$user_Register	= url_link('','','Register');
		$user_Forgot	= url_link('','','ForgotPassword');
		include("templates/".$_SESSION['current_tpl']."/boxes/user_guest.html");

	}
}

function box_category_menu() {
	global $mysql,$tb_prefix,$tpl,$web_type,$mainURL;
	$q = $mysql->query("SELECT cat_id, cat_name FROM ".$tb_prefix."cat WHERE sub_id IS NULL OR sub_id = 0 ORDER BY cat_order ASC");
	while ($r = $mysql->fetch_array($q)) {
		$cat_parent_NAME = $r['cat_name'];
		$cat_parent_URL	 = (m_get_config('url_type') == 0)?"index.php?go=list&show=list&cat_id=".$r['cat_id']."":$mainURL.'/Cat/'.$r['cat_id'].'/'.name_on_bar($cat_parent_NAME).$web_type;
		$cat_parent_HTML = "<div style=background-color:#F9F9F9>- <a href=$cat_parent_URL><b>$cat_parent_NAME</b></a></div>";
		$q2 = $mysql->query("SELECT cat_id, cat_name FROM ".$tb_prefix."cat WHERE sub_id = '".$r['cat_id']."' ORDER BY cat_order ASC");
		while ($r2 = $mysql->fetch_array($q2)) {		
			$cat_sub_NAME 	= $r2['cat_name'];
			$cat_sub_URL	= (m_get_config('url_type') == 0)?"index.php?go=list&show=list&cat_id=".$r2['cat_id']."":$mainURL.'/Cat/'.$r2['cat_id'].'/'.name_on_bar($cat_sub_NAME).$web_type;
			$cat_sub_HTML   = "&nbsp; --- <a href=$cat_sub_URL> $cat_sub_NAME</a><br>";		
		}
	$cat_HTML .= "$cat_parent_HTML$cat_sub_HTML";
	}
include("templates/".$_SESSION['current_tpl']."/boxes/category_menu.html");
}

function box_album($type,$number,$html) {
	global $mysql,$tb_prefix,$tpl,$web_type,$mainURL;
	if ($type == 'New') {
		$result = $mysql->query("SELECT album_id, album_name, album_singer, album_img FROM ".$tb_prefix."album ORDER BY album_id DESC LIMIT $number");
	}
	while ($r = $mysql->fetch_array($result)) {
		$album_ID	= $r['album_id'];
		$album_NAME = $r['album_name'];
		$album_IMG 	= m_get_img('Album',$r['album_img']);
		
		$singer_ID	= $r['album_singer'];
		$singer_NAME= m_get_data('SINGER',$r['album_singer']);

		$album_URL	= url_link($album_ID,$album_NAME,'Album');
		$singer_URL	= url_link($singer_ID,$singer_NAME,'Album');
		$album_HTML .= "<table><td>
			<tr><td rowspan=2><img src='$album_IMG' width=50 height=50></td><td><b><a href='$album_URL'>$album_NAME</a></b></td></tr>
			<tr><td><a href='$singer_URL'>$singer_NAME</a></td></tr>
		</td></table>";
	}
	include("templates/".$_SESSION['current_tpl']."/boxes/$html.html");
}
function box_stats() {
	global $mysql,$tb_prefix,$tpl;
	$r = $mysql->fetch_array($mysql->query("SELECT SUM(m.m_viewed) views, COUNT(m.m_id) songs, SUM(m.m_downloaded) downloads FROM ".$tb_prefix."data m"));
	extract($r);
	$r = $mysql->fetch_array($mysql->query("SELECT COUNT(singer_id) singers FROM ".$tb_prefix."singer"));
	extract($r);
	$r = $mysql->fetch_array($mysql->query("SELECT COUNT(user_id) users FROM ".$tb_prefix."user"));
	extract($r);
	$r = $mysql->fetch_array($mysql->query("SELECT COUNT(album_id) albums FROM ".$tb_prefix."album"));
	extract($r);
	$new = $mysql->fetch_array($mysql->query("SELECT * FROM ".$tb_prefix."user order by user_id desc limit 0,1"));
	$new1 = $new['user_name'];
	$newID = $new['user_id'];
	$member		=	m_get_data('USER',$newID);
	$member_url	=	url_link($newID,$member,'User');		
	$stat_SINGERS	=	$singers;
	$stat_SONGS		=	$songs;
	$stat_ALBUMS	=	$albums;
	$stat_USERS		=	$users;
	$stat_VIEWS		=	max(0,$views);
	$stat_DOWNLOADS	=	max(0,$downloads);
	$stat_COUNTER	=	m_counter();
	$stat_NEW 		=   $new1;
	$stat_member_url =   $member_url;	
include("templates/".$_SESSION['current_tpl']."/boxes/stats.html");
}

function box_tpl_list() {
	global $mysql,$tpl,$tb_prefix;
	$q = $mysql->query("SELECT * FROM ".$tb_prefix."tpl ORDER BY 'tpl_order' ASC");
	while($r = $mysql->fetch_array($q)) 
		$list .= "	<option id=template name=template value='".$r['tpl_sname']."'".(($_SESSION['current_tpl']==$r['tpl_sname'])?' selected':'').">".$r['tpl_fname']."</option>";
	include("templates/".$_SESSION['current_tpl']."/boxes/tpl_list.html");
}

function box_announcement() {
	global $mysql, $tpl;
	$contents = m_text_tidy(stripslashes(m_get_config('announcement')));
	if(!$contents) {}
	if($contents)
	include("templates/".$_SESSION['current_tpl']."/boxes/announcement.html");
}


function box_singer_list($type, $file_tpl) {
	global $mysql,$tb_prefix,$tpl,$web_type,$mainURL;
	$q = $mysql->query("SELECT * FROM ".$tb_prefix."singer WHERE singer_type = '".$type."' ORDER BY singer_name ASC");
	switch ($type) {
		case 1 :
			if (!$file_tpl) $file_tpl = 'singer_vn';
			$block = 'vnsinger';
			$unknownID = -1;
			$name	   = "VN";
		break;
		case 2 :
			if (!$file_tpl) $file_tpl = 'singer_fr';
			$block = 'frsinger';
			$unknownID = -2;
			$name	   = "QT";
		break;
	}
	$singer_NAME_unknown = 'Chưa biết';
	$singer_URL_unknown	 = url_link($unknownID,$name,'SingerUnknown');
	while ($r = $mysql->fetch_array($q)) {
		$singer_ID   = $r['singer_id'];
		$singer_NAME = $r['singer_name'];
		$singer_URL	 = url_link($singer_ID,$singer_NAME,'Singer');
		$singer_HTML .= "<div style=background-color:#FBFBFB;border-bottom: 2px solid #FFF><a href='$singer_URL'>$singer_NAME</a></div>";
	}
	include("templates/".$_SESSION['current_tpl']."/boxes/$file_tpl.html");
}

function box_top_media($type,$number) {
	global $mysql,$tb_prefix,$tpl,$web_type,$mainURL,$isLoggedIn;
	if ($type == 'Download_Month') {
		$result = $mysql->query("SELECT m_id, m_title, m_downloaded_month, m_poster FROM ".$tb_prefix."data WHERE m_downloaded_month > 0 ORDER BY m_downloaded_month DESC LIMIT ".$number);
		$block = 'top_download';
	}
	elseif ($type == 'Download') {
		$result = $mysql->query("SELECT m_id, m_title, m_downloaded, m_poster FROM ".$tb_prefix."data WHERE m_downloaded > 0 ORDER BY m_downloaded DESC LIMIT ".$number);
		$block = 'top_download';
	}
	elseif ($type == 'Play_Month') {
		$result = $mysql->query("SELECT m_id, m_title, m_viewed, m_poster FROM ".$tb_prefix."data WHERE m_viewed_month > 0 ORDER BY m_viewed_month DESC LIMIT ".$number);
		$block = 'top_play_month';
	}
	
	elseif ($type == 'Play') {
		$result = $mysql->query("SELECT m_id, m_title, m_viewed, m_poster FROM ".$tb_prefix."data WHERE m_viewed > 0 ORDER BY m_viewed DESC LIMIT ".$number);
		$block = 'top_play_play';
	}
	elseif ($type == 'Play_Week') {
		$result = $mysql->query("SELECT m_id, m_title, m_viewed, m_poster FROM ".$tb_prefix."data WHERE m_viewed_week > 0 ORDER BY m_viewed_week DESC LIMIT ".$number);
		$block = 'top_play_week';
	}
	elseif ($type == 'Play_Day') {
		$result = $mysql->query("SELECT m_id, m_title, m_viewed, m_poster FROM ".$tb_prefix."data WHERE m_viewed_day > 0 ORDER BY m_viewed_day DESC LIMIT ".$number);
		$block = 'top_play_day';
	}	
	elseif ($type == 'Newest') {
		$result = $mysql->query("SELECT m_id, m_title, m_viewed, m_poster FROM ".$tb_prefix."data ORDER BY m_id DESC LIMIT ".$number);
		$block = 'top_newest';
	}
	elseif ($type == 'Rate_week') {
		$result = $mysql->query("SELECT m_id, m_title, m_viewed, m_poster FROM ".$tb_prefix."data ORDER BY m_rating_total_week DESC LIMIT ".$number);
		$block = 'top_rate_week';
	}	
	$n = 0;
	if (!$mysql->num_rows($result)) $html = "<li><p class='title'>Chưa có</p></li>";
	else
		while ($r = $mysql->fetch_array($result)) {
			$n++;
			$song_ID 		= $r['m_id'];
			$song_TITLE 	= $r['m_title'];
			$user_ID 	= $r['m_poster'];
			$m_viewed 	= $r['m_viewed'];
			$user_NAME =  m_get_data('USER',$user_ID);
			if($user_NAME == '')
			{
				$user_NAME = 'DJ Other';
			}
		
			$m_downloaded_month 	= $r['m_downloaded_month'];				
			$song_URL		= url_link($song_ID,$song_TITLE,'nhac-san');
			$song_NUMBER	= sprintf('%0'.strlen($number).'d',$n);
			$top_MONTH 		=  m_get_config('current_month');
			$top_DAY		=  m_get_config('current_day');
			$top_WEEK 		=  m_get_config('current_week');
			$user_URL	= url_link($user_ID,$user_NAME,'User');			
			$song_TITLE1 = shortname($song_TITLE,80);
			$addp = 'addToPlaylist('.$song_ID.');return false;';	
			if (!$isLoggedIn){
				$addp = 'alert("Yêu cầu đăng nhập")';
			}				
			if ($type == 'Download_Month') 
			{
				$song_HTML	   .= " <li><p class='right'><span class='add_pl'><a href='#' onclick='$addp' title='Add to Playlist'></a></span></p></span><p class='left'><span class='num n$song_NUMBER'>$song_NUMBER.</span></p>
								<p class='title'><a href='$song_URL' title='$song_TITLE'>$song_TITLE1</a></p><p class='user'><a href='$user_URL'>$user_NAME</a></p></li>";
			}
			
			else {
			$song_HTML	   .= " <li><p class='right'><span class='add_pl'><a href='#' onclick='$addp' title='Add to Playlist'></a></span></p></span><p class='left'><span class='num n$song_NUMBER'>$song_NUMBER.</span></p>
								<p class='title'><a href='$song_URL' title='$song_TITLE'>$song_TITLE1</a></p><p class='user'><a href='$user_URL'>$user_NAME</a></p></li>";
			}
		}
		include("templates/".$_SESSION['current_tpl']."/boxes/$block.html");
}

function box_playlist($reload = false) {
	global $mysql, $tpl, $isLoggedIn, $tb_prefix, $add_id, $remove_id, $web_type, $mainURL;
	if ($isLoggedIn) {
		$user_name = m_get_data('USER',$_SESSION['user_id']);
                
                //Get all playlist for user
                $q = $mysql->query('SELECT playlist_id, playlist_name FROM '.$tb_prefix.'playlist WHERE playlist_user_id='.$_SESSION['user_id']);
                $list_playlist = '';
                while($r = $mysql->fetch_array($q)) {
                    $list_playlist .= '<option value='.$r['playlist_id'].'>'.$r['playlist_name'].'</option>';
                }
//		$playlist = m_get_data('PLAYLIST',$playlist_id);
//		$playlist = trim($playlist,',');
//		if ($playlist) {
//			$q = $mysql->query("SELECT m_id, m_title, m_viewed, m_poster FROM ".$tb_prefix."data WHERE m_id IN (".$playlist.")");
//			$i = 1;
//			while ($r = $mysql->fetch_array($q)) {
//				$song_ID 		= $r['m_id'];
//				$song_TITLE 	= $r['m_title'];
//				$user_ID 	= $r['m_poster'];
//				$user_NAME1 =  m_get_data('USER',$user_ID);
//				if($user_NAME1 == '')
//				{
//					$user_NAME1 = 'DJ Other';
//				}
//		
//				$user_URL	= url_link($user_ID,$user_NAME1,'User');	
//				$m_viewed 	= $r['m_viewed'];	
//				$song_TITLE1 = shortname($song_TITLE,40);				
//				$song_URL		= url_link($song_ID,$song_TITLE,'nhac-san');
//				$playlist_URL	= url_link($playlist_id,$user_name,'Playlist');		
//				$play_ALL 		= ($_SESSION['user_id'])?"<p style='margin-top:20px;margin-left:85px;'> <a style='color:#89014b;' href=$playlist_URL><b>Nghe Playlist</b></a></p>":'';
//				$playlist_HTML .= "<li><p class='right'><span class='del'><a href=# onClick='removeFromPlaylist($song_ID)' title='remove from Playlist'></a></span> </p> <p class='title'>$i. <a href='$song_URL' title='$song_TITLE'>$song_TITLE1</a></p><p class='user'>Đăng bởi: <a href='$user_URL'>$user_NAME1</a></p></li>";
//				$i++;
//			}
//		}
//
//		
//		
//		
                if ($list_playlist != '') {
                    $play_ALL 		= '';
                    $playlist_HTML = "<li><p class='title'>Bạn cần chọn 1 playlist</a></p></li>";
                } else {
                    $play_ALL 		= '';
                    $playlist_HTML = "<li><p class='title'>Bạn chưa có playlist</a></p></li>";
		}
	}
	else {
		$play_ALL 		= '';
		$playlist_HTML = "<li><p class='title'>Bạn cần đăng nhập</a></p></li>";
	}	
include("templates/".$_SESSION['current_tpl']."/boxes/playlist.html");
}

function box_ads() {
	global $mysql,$tb_prefix,$tpl;
	$result = $mysql->query("SELECT * FROM ".$tb_prefix."ads ORDER BY ads_count DESC");	
	if (!$mysql->num_rows($result)) $ads_HTML = "Chưa có";
	while ($r = $mysql->fetch_array($result)) {
		$ads_IMG	=	$r['ads_img'];
		$ads_URL	=	$r['ads_url'];
		$ads_WEB	=	$r['ads_web'];
		$ads_HTML  .=   "<div><a href='$ads_URL' target=_blank><img width=160 height=80 src='$ads_IMG' title='$ads_WEB' alt='$ads_WEB'></a></div>";
	}
include("templates/".$_SESSION['current_tpl']."/boxes/ads.html");
}
function box_randompost($id) {
	global $mysql,$tb_prefix,$tpl,$isLoggedIn;
	$query = $mysql->query("SELECT m_cat,m_poster FROM ".$tb_prefix."data WHERE m_id = ".$id);
	if($mysql->num_rows($query)) {
		$row = $mysql->fetch_array($query);
		//$catID = $row['m_cat'];
		//$title = m_get_data('CAT',$catID);	
		$user_ID1 = $row['m_poster'];
		$user_NAME =  m_get_data('USER',$user_ID1);		
	}	
	$sql_random = "SELECT * FROM ".$tb_prefix."data WHERE m_poster = $user_ID1 ORDER BY m_id DESC limit 10";
	$query_random = $mysql->query($sql_random);
	if($mysql->num_rows($query_random)) {
		while ($r = $mysql->fetch_array($query_random)) {
			$song_ID = $r['m_id'];
			$song_TITLE = $r['m_title'];
			$song_VIEWED = $r['m_viewed'];
			$user_ID = $r['m_poster'];
			$user_NAME =  m_get_data('USER',$user_ID);
			$song_TITLE1 = $song_TITLE;
			$catID = $r['m_cat'];
			$cat_NAME = m_get_data('CAT',$catID);	
			$cat_URL    = url_link($catID,$cat_NAME,'Cat');			

			if($user_NAME == '')
				{
					$user_NAME = 'DJ Other';
				}
			$addp = 'addToPlaylist('.$song_ID.');return false;';	
			if (!$isLoggedIn){
				$addp = 'alert("Yêu cầu đăng nhập")';
			}				
			$user_URL	= url_link($user_ID,$user_NAME,'User');			
			$m_rate = $r['m_rating'];
			$m_rate_total = $r['m_rating_total'];
			if ($m_rate =='0') $current_star = 0;

			else $rater_rating = $m_rate / $m_rate_total;
			if ($rater_rating <= 0  ) $star = 0;
			if ($rater_rating >= 0.5) $star = 1;
    		if ($rater_rating >= 1) $star = 2;
			if ($rater_rating >= 1.5) $star = 3;
			if ($rater_rating >= 2) $star = 4;
			if ($rater_rating >= 2.5) $star = 5;
			if ($rater_rating >= 3) $star = 6;
			if ($rater_rating >= 3.5) $star = 7;
			if ($rater_rating >= 4) $star = 8;
			if ($rater_rating >= 4.5) $star = 9;
			if ($rater_rating >= 5) $star = 10;			
			$song_URL = url_link($song_ID,$song_TITLE,'nhac-san');
			$random_HTML .= "<li><p class='right'><span class='rate s_".$star."'><span><span><span><span><span></span></span></span></span></span></span><span class='add_pl'><a href='#' onclick='$addp' title='Add to Playlist'></a></span></p><p class='title'><a href='$song_URL' title='$song_TITLE'>$song_TITLE1</a></p><p class='user'><p class='user'>Đăng bởi: <a href='$user_URL'>$user_NAME</a> | <a href='$cat_URL'>$cat_NAME</a> | Lượt nghe: $song_VIEWED</p></li>";		}	
	}
	else{
		 $random_HTML = '<li><p class="title">Chưa có !</p></li>';

	}

include("templates/".$_SESSION['current_tpl']."/boxes/random_post.html");
}
?>
